package hotel.meituan.oversea.biz.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import hotel.base.oversea.utils.DateUtil;
import hotel.base.oversea.utils.Result;
import hotel.meituan.oversea.api.dto.MtPriceAndStatusDto;
import hotel.meituan.oversea.api.entity.MtHotelPriceStatisLog;
import hotel.meituan.oversea.api.vo.StatisLogVo;
import hotel.meituan.oversea.biz.mapper.MtHotelGoodsStatusChangeLogMapper;
import hotel.meituan.oversea.biz.mapper.MtHotelPriceStatisLogMapper;
import hotel.meituan.oversea.biz.service.MtHotelPriceStatisLogService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Auther: lmf
 * @Date: 2021/01/12/15:08
 * @Description:
 */
@Service
@Slf4j
public class MtHotelPriceStatisLogServiceImpl implements MtHotelPriceStatisLogService {

    @Autowired
    private MtHotelGoodsStatusChangeLogMapper mtHotelGoodsStatusChangeLogMapper;
    @Autowired
    private MtHotelPriceStatisLogMapper mtHotelPriceStatisLogMapper;

    /***
    * @Author: lmf
    * @Date: 2021/1/12
     * 统计每周价格房态的数据
    */
    @SneakyThrows
    @Override
    public Result<List<MtHotelPriceStatisLog>> statisWeekRooomDataList(Integer genre) {
        String time=DateUtil.getAgoDays(7);
        log.info("==========七日前的时间为{}============",time);
        List<MtHotelPriceStatisLog> mtPriceAndStatusDtos=new ArrayList<>();
        if(genre==0){//统计每日价格  0 主动价格
            new Thread(() -> {
                try {
                    statisWeekRooomDataListByPrice();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }).start();
            mtPriceAndStatusDtos=mtHotelPriceStatisLogMapper.selectByWeekDataByGenre(genre,time);
            return new Result<>(mtPriceAndStatusDtos);
        }else if(genre==1){//统计异步价格
            new Thread(() -> {
                try {
                    statisWeekRooomDataListByAsyncPrice();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }).start();
            mtPriceAndStatusDtos=mtHotelPriceStatisLogMapper.selectByWeekDataByGenre(genre,time);
            return new Result<>(mtPriceAndStatusDtos);
        }else if(genre==2){//主动房态
            new Thread(() -> {
                try {
                    statisWeekRoomDataListByStatus();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
            mtPriceAndStatusDtos=mtHotelPriceStatisLogMapper.selectByWeekDataByGenre(genre,time);
            return new Result<>(mtPriceAndStatusDtos);
        }else{
            new Thread(() -> {
                try {
                    statisWeekRoomDataListByAsyncStatus();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
            mtPriceAndStatusDtos=mtHotelPriceStatisLogMapper.selectByWeekDataByGenre(genre,time);
            return new Result<>(mtPriceAndStatusDtos);
        }
    }

    /***
    * @Author: lmf
    * @Date: 2021/1/12
     * 统计主动价格记录
    */
    public Result<List<MtPriceAndStatusDto>> statisWeekRooomDataListByPrice() throws ParseException {
        log.info("============主动查询一个礼拜价格的变化数据量=================");
        //主动价格获取一个礼拜前的今日
        String time= DateUtil.getAgoDays(1);
        List<MtPriceAndStatusDto> list=mtHotelGoodsStatusChangeLogMapper.statisWeekStatusDataList(time);
        List<MtHotelPriceStatisLog> mtHotelPriceStatisLogs=new ArrayList<>();
        for(MtPriceAndStatusDto mtPriceAndStatusDto:list){
            log.info("============遍历主动价格的数据中====================");
            MtHotelPriceStatisLog mtHotelPriceStatisLog=new MtHotelPriceStatisLog();
            mtHotelPriceStatisLog.setGenre(0);
            mtHotelPriceStatisLog.setTime(mtPriceAndStatusDto.getTime());
            int count=mtHotelPriceStatisLogMapper.selectCount(mtHotelPriceStatisLog);
            if(count<1){
                mtHotelPriceStatisLog.setNum(mtPriceAndStatusDto.getNum());
                mtHotelPriceStatisLogs.add(mtHotelPriceStatisLog);
            }else{
                mtHotelPriceStatisLog.setNum(mtPriceAndStatusDto.getNum());
                Example example=new Example(MtHotelPriceStatisLog.class);
                Example.Criteria criteria=example.createCriteria();
                criteria.andEqualTo("time",mtPriceAndStatusDto.getTime());
                criteria.andEqualTo("genre",0);
                mtHotelPriceStatisLogMapper.updateByExample(mtHotelPriceStatisLog,example);
            }
            log.info("==========数据条数为{}=====",mtPriceAndStatusDto.getNum());
        }
        if(!ObjectUtils.isEmpty(mtHotelPriceStatisLogs)&&mtHotelPriceStatisLogs.size()>0){
            mtHotelPriceStatisLogMapper.insertList(mtHotelPriceStatisLogs);
        }
        return null;
    }
    /***
    * @Author: lmf
    * @Date: 2021/1/18
     * 获取当前时间戳
    */
    private static Date getStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.DAY_OF_WEEK,-1);
        todayStart.set(Calendar.HOUR, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    private static Date getStartTimes() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.DATE,-1);
        todayStart.set(Calendar.HOUR, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }
    public static Date getBeforeDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = calendar.getTime();
        return date;
    }
    public static Date getToday(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 0);
        date = calendar.getTime();
        return date;
    }


        /***
        * @Author: lmf
        * @Date: 2021/1/12
         * 统计异步每日价格
        */
    public Result<List<MtPriceAndStatusDto>> statisWeekRooomDataListByAsyncPrice() throws ParseException {
        log.info("============异步查询一个礼拜价格的变化数据量=================");
        //主动价格获取一个礼拜前的今日
        List<String> tableName=new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String time= sdf.format(getBeforeDay(new Date()));
        String name="mt_hotel_goods_info_notify_log_"+time;
        tableName.add(name);
        time= sdf.format(getToday(new Date()));
        name="mt_hotel_goods_info_notify_log_"+time;
        tableName.add(name);
        log.info("============查询的日期为{}====", JSON.toJSONString(tableName));
        List<MtPriceAndStatusDto> list=mtHotelGoodsStatusChangeLogMapper.selectPriceDataByTime(tableName);
        List<MtHotelPriceStatisLog> mtHotelPriceStatisLogs=new ArrayList<>();
        for(MtPriceAndStatusDto mtPriceAndStatusDto:list){
            MtHotelPriceStatisLog mtHotelPriceStatisLog=new MtHotelPriceStatisLog();
            mtHotelPriceStatisLog.setGenre(1);
            mtHotelPriceStatisLog.setTime(mtPriceAndStatusDto.getTime());
            int count=mtHotelPriceStatisLogMapper.selectCount(mtHotelPriceStatisLog);
            if(count<1){
                mtHotelPriceStatisLog.setNum(mtPriceAndStatusDto.getNum());
                mtHotelPriceStatisLogs.add(mtHotelPriceStatisLog);
            }else{
                mtHotelPriceStatisLog.setNum(mtPriceAndStatusDto.getNum());
                Example example=new Example(MtHotelPriceStatisLog.class);
                Example.Criteria criteria=example.createCriteria();
                criteria.andEqualTo("time",mtPriceAndStatusDto.getTime());
                criteria.andEqualTo("genre",1);
                mtHotelPriceStatisLogMapper.updateByExample(mtHotelPriceStatisLog,example);
            }
        }
        if(!ObjectUtils.isEmpty(mtHotelPriceStatisLogs)&&mtHotelPriceStatisLogs.size()>0){
            mtHotelPriceStatisLogMapper.insertList(mtHotelPriceStatisLogs);
        }
        return new Result<>();
    }

    /***
    * @Author: lmf
    * @Date: 2021/1/12
     * 统计一个礼拜异步房态数据
    */
    public Result<List<MtPriceAndStatusDto>> statisWeekRoomDataListByAsyncStatus(){
        log.info("==============异步房态数据====================");
        //主动价格获取一个礼拜前的今日
        String time= DateUtil.getAgoDays(1);
        List<MtPriceAndStatusDto> list=mtHotelGoodsStatusChangeLogMapper.statisWeekRoomDataListByAsyncStatus(time);
        List<MtHotelPriceStatisLog> mtHotelPriceStatisLogs=new ArrayList<>();
        for(MtPriceAndStatusDto mtPriceAndStatusDto:list){
            MtHotelPriceStatisLog mtHotelPriceStatisLog=new MtHotelPriceStatisLog();
            mtHotelPriceStatisLog.setGenre(3);
            mtHotelPriceStatisLog.setTime(mtPriceAndStatusDto.getTime());
            int count=mtHotelPriceStatisLogMapper.selectCount(mtHotelPriceStatisLog);
            if(count<1){
                mtHotelPriceStatisLog.setNum(mtPriceAndStatusDto.getNum());
                mtHotelPriceStatisLogs.add(mtHotelPriceStatisLog);
            }else{
                mtHotelPriceStatisLog.setNum(mtPriceAndStatusDto.getNum());
                Example example=new Example(MtHotelPriceStatisLog.class);
                Example.Criteria criteria=example.createCriteria();
                criteria.andEqualTo("time",mtPriceAndStatusDto.getTime());
                criteria.andEqualTo("genre",3);
                mtHotelPriceStatisLogMapper.updateByExample(mtHotelPriceStatisLog,example);
            }
        }
        if(!ObjectUtils.isEmpty(mtHotelPriceStatisLogs)&&mtHotelPriceStatisLogs.size()>0){
            mtHotelPriceStatisLogMapper.insertList(mtHotelPriceStatisLogs);
        }
        return new Result<>();
    }
    /***
    * @Author: lmf
    * @Date: 2021/1/12
     * 统计一个礼拜主动房态数据
    */
    public Result<List<MtPriceAndStatusDto>> statisWeekRoomDataListByStatus(){
        log.info("==============主动房态数据====================");
        //主动价格获取一个礼拜前的今日
        String time= DateUtil.getAgoDays(1);
        List<MtPriceAndStatusDto> list=mtHotelGoodsStatusChangeLogMapper.statisWeekRoomDataListByStatus(time);
        List<MtHotelPriceStatisLog> mtHotelPriceStatisLogs=new ArrayList<>();
        for(MtPriceAndStatusDto mtPriceAndStatusDto:list){
            MtHotelPriceStatisLog mtHotelPriceStatisLog=new MtHotelPriceStatisLog();
            mtHotelPriceStatisLog.setGenre(2);
            mtHotelPriceStatisLog.setTime(mtPriceAndStatusDto.getTime());
            int count=mtHotelPriceStatisLogMapper.selectCount(mtHotelPriceStatisLog);
            if(count<1){
                mtHotelPriceStatisLog.setNum(mtPriceAndStatusDto.getNum());
                mtHotelPriceStatisLogs.add(mtHotelPriceStatisLog);
            }else{
                mtHotelPriceStatisLog.setNum(mtPriceAndStatusDto.getNum());
                Example example=new Example(MtHotelPriceStatisLog.class);
                Example.Criteria criteria=example.createCriteria();
                criteria.andEqualTo("time",mtPriceAndStatusDto.getTime());
                criteria.andEqualTo("genre",2);
                mtHotelPriceStatisLogMapper.updateByExample(mtHotelPriceStatisLog,example);
            }
        }
        if(!ObjectUtils.isEmpty(mtHotelPriceStatisLogs)&&mtHotelPriceStatisLogs.size()>0){
            mtHotelPriceStatisLogMapper.insertList(mtHotelPriceStatisLogs);
        }
        return new Result<>();
    }

    /***
    * @Author: lmf
    * @Date: 2021/1/18
     * 添加统计日志到表里
    */
    @Override
    public Result<List<MtHotelPriceStatisLog>> addStatisDataLog(StatisLogVo statisLogVo) {
        List<MtHotelPriceStatisLog> mtHotelPriceStatisLogs=new ArrayList<>();
        //主动价格
        if(statisLogVo.getGenre()==0){
            List<MtPriceAndStatusDto> list=mtHotelGoodsStatusChangeLogMapper.staticTimeData(statisLogVo.getTime(),statisLogVo.getEndTime());
            if(ObjectUtils.isEmpty(list)||list.size()<1){
                return new Result<>();
            }
            for(MtPriceAndStatusDto mtPriceAndStatusDto:list){
                log.info("============遍历主动价格的数据中====================");
                MtHotelPriceStatisLog mtHotelPriceStatisLog=new MtHotelPriceStatisLog();
                mtHotelPriceStatisLog.setGenre(0);
                mtHotelPriceStatisLog.setTime(mtPriceAndStatusDto.getTime());
                int count=mtHotelPriceStatisLogMapper.selectCount(mtHotelPriceStatisLog);
                if(count<1){
                    mtHotelPriceStatisLog.setNum(mtPriceAndStatusDto.getNum());
                    mtHotelPriceStatisLogs.add(mtHotelPriceStatisLog);
                }else{
                    mtHotelPriceStatisLog.setNum(mtPriceAndStatusDto.getNum());
                    Example example=new Example(MtHotelPriceStatisLog.class);
                    Example.Criteria criteria=example.createCriteria();
                    criteria.andEqualTo("time",mtPriceAndStatusDto.getTime());
                    criteria.andEqualTo("genre",0);
                    mtHotelPriceStatisLogMapper.updateByExample(mtHotelPriceStatisLog,example);
                }
                log.info("==========数据条数为{}=====",mtPriceAndStatusDto.getNum());
            }
            if(!ObjectUtils.isEmpty(mtHotelPriceStatisLogs)&&mtHotelPriceStatisLogs.size()>0){
                mtHotelPriceStatisLogMapper.insertList(mtHotelPriceStatisLogs);
            }
            return new Result<>();
        }else if(statisLogVo.getGenre()==1){//异步价格
            String name="mt_hotel_goods_info_notify_log_"+statisLogVo.getTableTime();
            List<MtPriceAndStatusDto> list=mtHotelGoodsStatusChangeLogMapper.selectTableNameData(name);
            if(ObjectUtils.isEmpty(list)||list.size()<1){
                return new Result<>();
            }
            for(MtPriceAndStatusDto mtPriceAndStatusDto:list){
                MtHotelPriceStatisLog mtHotelPriceStatisLog=new MtHotelPriceStatisLog();
                mtHotelPriceStatisLog.setGenre(1);
                mtHotelPriceStatisLog.setTime(mtPriceAndStatusDto.getTime());
                int count=mtHotelPriceStatisLogMapper.selectCount(mtHotelPriceStatisLog);
                if(count<1){
                    mtHotelPriceStatisLog.setNum(mtPriceAndStatusDto.getNum());
                    mtHotelPriceStatisLogs.add(mtHotelPriceStatisLog);
                }else{
                    mtHotelPriceStatisLog.setNum(mtPriceAndStatusDto.getNum());
                    Example example=new Example(MtHotelPriceStatisLog.class);
                    Example.Criteria criteria=example.createCriteria();
                    criteria.andEqualTo("time",mtPriceAndStatusDto.getTime());
                    criteria.andEqualTo("genre",1);
                    mtHotelPriceStatisLogMapper.updateByExample(mtHotelPriceStatisLog,example);
                }
            }
            if(!ObjectUtils.isEmpty(mtHotelPriceStatisLogs)&&mtHotelPriceStatisLogs.size()>0){
                mtHotelPriceStatisLogMapper.insertList(mtHotelPriceStatisLogs);
            }
            return new Result<>();
        }else if(statisLogVo.getGenre()==2){//主动房态
            List<MtPriceAndStatusDto> list=mtHotelGoodsStatusChangeLogMapper.staticTimeStatusData(statisLogVo.getTime(),statisLogVo.getEndTime());
            if(ObjectUtils.isEmpty(list)||list.size()<1){
                return new Result<>();
            }
            for(MtPriceAndStatusDto mtPriceAndStatusDto:list){
                MtHotelPriceStatisLog mtHotelPriceStatisLog=new MtHotelPriceStatisLog();
                mtHotelPriceStatisLog.setGenre(2);
                mtHotelPriceStatisLog.setTime(mtPriceAndStatusDto.getTime());
                int count=mtHotelPriceStatisLogMapper.selectCount(mtHotelPriceStatisLog);
                if(count<1){
                    mtHotelPriceStatisLog.setNum(mtPriceAndStatusDto.getNum());
                    mtHotelPriceStatisLogs.add(mtHotelPriceStatisLog);
                }else{
                    mtHotelPriceStatisLog.setNum(mtPriceAndStatusDto.getNum());
                    Example example=new Example(MtHotelPriceStatisLog.class);
                    Example.Criteria criteria=example.createCriteria();
                    criteria.andEqualTo("time",mtPriceAndStatusDto.getTime());
                    criteria.andEqualTo("genre",2);
                    mtHotelPriceStatisLogMapper.updateByExample(mtHotelPriceStatisLog,example);
                }
            }
            if(!ObjectUtils.isEmpty(mtHotelPriceStatisLogs)&&mtHotelPriceStatisLogs.size()>0){
                mtHotelPriceStatisLogMapper.insertList(mtHotelPriceStatisLogs);
            }
            return new Result<>();
        }else{//异步房态
            List<MtPriceAndStatusDto> list=mtHotelGoodsStatusChangeLogMapper.staticTimeStatusAsyData(statisLogVo.getTime(),statisLogVo.getEndTime());
            if(ObjectUtils.isEmpty(list)||list.size()<1){
                return new Result<>();
            }
            for(MtPriceAndStatusDto mtPriceAndStatusDto:list){
                MtHotelPriceStatisLog mtHotelPriceStatisLog=new MtHotelPriceStatisLog();
                mtHotelPriceStatisLog.setGenre(3);
                mtHotelPriceStatisLog.setTime(mtPriceAndStatusDto.getTime());
                int count=mtHotelPriceStatisLogMapper.selectCount(mtHotelPriceStatisLog);
                if(count<1){
                    mtHotelPriceStatisLog.setNum(mtPriceAndStatusDto.getNum());
                    mtHotelPriceStatisLogs.add(mtHotelPriceStatisLog);
                }else{
                    mtHotelPriceStatisLog.setNum(mtPriceAndStatusDto.getNum());
                    Example example=new Example(MtHotelPriceStatisLog.class);
                    Example.Criteria criteria=example.createCriteria();
                    criteria.andEqualTo("time",mtPriceAndStatusDto.getTime());
                    criteria.andEqualTo("genre",3);
                    mtHotelPriceStatisLogMapper.updateByExample(mtHotelPriceStatisLog,example);
                }
            }
            if(!ObjectUtils.isEmpty(mtHotelPriceStatisLogs)&&mtHotelPriceStatisLogs.size()>0){
                mtHotelPriceStatisLogMapper.insertList(mtHotelPriceStatisLogs);
            }
            return new Result<>();
        }
    }

}
